import 'dart:convert';

import 'package:my_shop/generated/json/base/json_convert_content.dart';
import 'package:my_shop/util/constant.dart';

/// API响应基类
class ApiResponse<T> {
  ApiResponse({
    required this.code,
    required this.msg,
    this.data,
  });

  /// 从JSON创建实例
  factory ApiResponse.fromJson(Map<String, dynamic> json) {
    return ApiResponse<T>(
      code: json[Constant.code] as int? ?? 500,
      msg: json[Constant.msg] as String? ?? '',
      data: json.containsKey(Constant.data) ? _generateData<T>(json[Constant.data]) : null,
    );
  }

  /// 状态码
  final int code;
  
  /// 消息
  final String msg;
  
  /// 数据
  final T? data;

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      Constant.code: code,
      Constant.msg: msg,
      Constant.data: data,
    };
  }

  /// 检查是否成功
  bool get isSuccess => code == 0;

  @override
  String toString() => jsonEncode(toJson());

  /// 生成数据对象
  static T? _generateData<T>(dynamic json) {
    if (T == String) {
      return json.toString() as T;
    } else if (T == Map<String, dynamic>) {
      return json as T;
    } else if (T == List) {
      //todo 遇到返回list的接口时看是否要处理嵌套的 List 类型
      final List<dynamic> list = json as List<dynamic>;
      return list.map((item) => JsonConvert.fromJsonAsT<Map<String, dynamic>>(item)).toList() as T;
      // return json as T;
    } else {
      T? result = JsonConvert.fromJsonAsT<T>(json);
      return result;
    }
  }
}

/// 用于处理列表类型的API响应
class ApiListResponse<T> extends ApiResponse<List<T>> {
  ApiListResponse({
    required super.code,
    required super.msg,
    List<T>? data,
  }) : super(data: data ?? []);

  /// 从JSON创建实例
  factory ApiListResponse.fromJson(Map<String, dynamic> json) {
    final List<dynamic>? jsonList = json[Constant.data] as List<dynamic>?;
    final List<T> items = [];
    if (jsonList != null) {
      for (final item in jsonList) {
        final converted = JsonConvert.fromJsonAsT<T>(item);
        if (converted != null) {
          items.add(converted);
        }
      }
    }
    return ApiListResponse<T>(
      code: json[Constant.code] as int? ?? 500,
      msg: json[Constant.msg] as String? ?? '',
      data: items,
    );
  }
}

/// 用于处理分页数据的API响应
class ApiPageResponse<T> extends ApiResponse<PageData<T>> {
  ApiPageResponse({
    required super.code,
    required super.msg,
    PageData<T>? data,
  }) : super(data: data);

  /// 从JSON创建实例
  factory ApiPageResponse.fromJson(Map<String, dynamic> json) {
    final Map<String, dynamic>? pageData = json[Constant.data] as Map<String, dynamic>?;
    return ApiPageResponse<T>(
      code: json[Constant.code] as int? ?? 500,
      msg: json[Constant.msg] as String? ?? '',
      data: pageData != null ? PageData<T>.fromJson(pageData) : null,
    );
  }
}

/// 分页数据
class PageData<T> {
  PageData({
    required this.list,
    required this.total,
    required this.page,
    required this.size,
  });

  /// 从JSON创建实例
  factory PageData.fromJson(Map<String, dynamic> json) {
    final List<dynamic> jsonList = json['list'] as List<dynamic>;
    final List<T> items = [];
    for (final item in jsonList) {
      final converted = JsonConvert.fromJsonAsT<T>(item);
      if (converted != null) {
        items.add(converted);
      }
    }
    return PageData<T>(
      list: items,
      total: json['total'] as int? ?? 0,
      page: json['page'] as int? ?? 1,
      size: json['size'] as int? ?? 10,
    );
  }

  /// 数据列表
  final List<T> list;
  
  /// 总数
  final int total;
  
  /// 当前页码
  final int page;
  
  /// 每页大小
  final int size;

  /// 是否有下一页
  bool get hasNext => (page * size) < total;

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'list': list,
      'total': total,
      'page': page,
      'size': size,
    };
  }
} 